﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Automation;

namespace CrazyWorshipTests
{
    class AutomationHelper
    {

        public static AutomationElement FindElementByID(String automationID, AutomationElement rootElement)
        {
            if ((automationID == "") || (rootElement == null))
            {
                throw new ArgumentException("Argument cannot be null or empty.");
            }
            // Set a property condition that will be used to find the control.

            Condition c = new PropertyCondition(AutomationElement.AutomationIdProperty, automationID, PropertyConditionFlags.IgnoreCase);
            // Find the element.
            return rootElement.FindFirst(TreeScope.Element | TreeScope.Children, c);
        }

        public static AutomationElement FindElementByName(String controlName, AutomationElement rootElement)
        {
            if ((controlName == "") || (rootElement == null))
            {
                throw new ArgumentException("Argument cannot be null or empty.");
            }
            // Set a property condition that will be used to find the control.
            Condition c = new PropertyCondition(
            AutomationElement.NameProperty, controlName,

            PropertyConditionFlags.IgnoreCase);

            // Find the element.
            return rootElement.FindFirst(TreeScope.Element | TreeScope.Children, c);
        }
        public static AutomationElement FindElementByType(ControlType controlType, AutomationElement rootElement)
        {
            if ((controlType == null) || (rootElement == null))
            {
                throw new ArgumentException("Argument cannot be null.");
            }

            // Set a property condition that will be used to find the control.
            Condition c = new PropertyCondition(
            AutomationElement.ControlTypeProperty, controlType);

            // Find the element.
            return rootElement.FindFirst(TreeScope.Element | TreeScope.Children, c);
        }

        public static AutomationElementCollection FindByMultipleConditions(AutomationElement rootElement)
        {
            if (rootElement == null)
            {
                throw new ArgumentException();
            }

            Condition c = new AndCondition(
            new PropertyCondition(AutomationElement.IsEnabledProperty, true),
            new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Button)
            );

            // Find all children that match the specified conditions.
            return rootElement.FindAll(TreeScope.Children, c);
        }

        public static AutomationElement FindListItemByIndex(AutomationElement parent, int index)
        {
            if (parent == null)
            {
                throw new ArgumentException();
            }

            Condition c = new AndCondition(
            new PropertyCondition(AutomationElement.IsControlElementProperty, true));

            // Find all children that match the specified conditions.
            AutomationElementCollection found = parent.FindAll(TreeScope.Children, c);
            return found[index];
        }
        //AutomationElement FindListItemByIndex(AutomationElement root, String type)
        //{
        //    if ((root == null) || (type == ""))
        //    {
        //        throw new ArgumentException("Argument cannot be null or empty.");
        //    }

        //    Condition c = new AndCondition(
        //    new PropertyCondition(AutomationElement.ClassNameProperty, type));

        //    // Find all children that match the specified conditions.
        //    AutomationElementCollection found = root.FindAll(TreeScope.Children, c);
        //    return found[index];
        //}
    }
}